package com.ddone;

import com.ddone.bean.Student;
import com.ddone.bean.StudentExample;
import com.ddone.mapper.StudentMapper;
import com.ddone.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/24-11:13
 */
public class MybatisSqlTest {
    SqlSession session;

    @Before
    public void init() {
        //  //1.获取sqlSession
        session = MybatisUtil.openAutoSession();
    }

    @After
    public void destroy(){
        session.close();
    }
    @Test
    public void testStudent() {
        //2.获取mapper
        StudentMapper mapper = session.getMapper(StudentMapper.class);

        // 整体查询
        StudentExample example = new StudentExample();
        List<Student> students = mapper.selectByExample(example);
        System.out.println("students = " + students);

        //TODO: 单个criteria之间and 多个之间是or关系
        //TODO: id 在 1 - 5  and age >= 18 or  u_account = null

        // 年龄大于27的学生
        StudentExample.Criteria criteria = example.createCriteria();
        criteria.andAgeGreaterThan(27);
        List<Student> students1 = mapper.selectByExample(example);

        System.out.println("students1 = " + students1);

        example.clear();
        example.createCriteria().andSexEqualTo("男").andAgeEqualTo(26);

        System.out.println("mapper.selectByExample(example) = " + mapper.selectByExample(example));
        // 单个条件用 and 多个条件用or
    }
}
